using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

namespace InstallWSPBuilder
{
    public class Program
    {
		private enum VisualStuidoVersion
		{
			VS05x86,
			VS05x64,
			VS08x86,
			VS08x64
		}

        static void Main(string[] args)
        {
			const string PROJECT_TEMPLATE_NAME = "MOSS Solution Builder Project.zip";
			string vsRegistryPath = string.Empty;

			VisualStuidoVersion vsVersion = VisualStuidoVersion.VS08x86;

			Console.WriteLine("WSP Project Template for Visual Stuido Configuration Tool");
			Console.WriteLine("");

			Console.WriteLine("First, we need a little info from you.");
			Console.WriteLine("");

			string vsInstallVersion = GetVSVersion();

			Console.WriteLine("");

			string vsBitVersion = GetVSBitVersion();

			Console.WriteLine("");

			string fullVSVersion = vsInstallVersion.ToUpper() + vsBitVersion.ToUpper();
			string visualStudioVersion = string.Empty;

			
			
			switch(fullVSVersion)
			{
				case "2005X86":
					vsVersion = VisualStuidoVersion.VS05x86;
					vsRegistryPath = "SOFTWARE\\Microsoft\\VisualStudio\\8.0";
					visualStudioVersion = "2005 x86";
					break;
				case "2005X64":
					vsVersion = VisualStuidoVersion.VS05x64;
					vsRegistryPath = "SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\8.0";
					visualStudioVersion = "2005 x64";
					break;
				case "2008X64":
					vsVersion = VisualStuidoVersion.VS08x64;
					vsRegistryPath = "SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\9.0";
					visualStudioVersion = "2008 x64";
					break;
				case "2008X86":
				default:
					vsVersion = VisualStuidoVersion.VS08x86;
					vsRegistryPath = "SOFTWARE\\Microsoft\\VisualStudio\\9.0";
					visualStudioVersion = "2008 x86";
					break;
			}
            

            string vsRegInstallPath = string.Empty;

			RegistryKey vsInstallKey = null;

			Console.WriteLine("Searching registry for visual studio.");
			Console.WriteLine("This is done so you don't need to tell where you installed it.");
			vsInstallKey = Registry.LocalMachine.OpenSubKey(vsRegistryPath);
			string vsInstallPath;
			try
			{
				vsInstallPath = vsInstallKey.GetValue("InstallDir") as string;
				Console.WriteLine("Found Visual Studio Registry Path. Getting install directory...");
			}
			catch
			{
				Console.WriteLine("");
				Console.WriteLine("Could not find visual studio in registry. This is bad. Now exiting...");
				Console.WriteLine("Press any key to continue");
				Console.ReadLine();
				return;
			}

            
			Console.WriteLine("Found VS installation directory");
			Console.WriteLine("Yours is: " + vsInstallPath);

            DirectoryInfo IDE = new DirectoryInfo(vsInstallPath);
            string projectTemplatesPath = IDE.FullName + "\\ProjectTemplates";
            DirectoryInfo projectTemplates = new DirectoryInfo(projectTemplatesPath);
            string mossFinnaticsPath = projectTemplatesPath + "\\SharePoint_RF";

            DirectoryInfo mossFinnatics = new DirectoryInfo(mossFinnaticsPath);
            if (!mossFinnatics.Exists)
            {
                mossFinnatics = projectTemplates.CreateSubdirectory("SharePoint_RF");
            }

            string thisDirectoryPath = Directory.GetCurrentDirectory();
            DirectoryInfo thisDirectory = new DirectoryInfo(thisDirectoryPath);
            FileInfo[] zipFiles = thisDirectory.GetFiles("*.zip");
            FileInfo projectTemplate = null;

            foreach(FileInfo fileinfo in zipFiles)
            {
                if(fileinfo.Name == PROJECT_TEMPLATE_NAME)
                {
                    projectTemplate = fileinfo;
                    break;
                }
            }

            projectTemplate.CopyTo(mossFinnatics.FullName + "\\" + PROJECT_TEMPLATE_NAME,true);

			Console.WriteLine("Configuration Visual Stuido " + visualStudioVersion);
			Console.WriteLine("Please wait. This could take a few minutes");

            Process myProc;
            // execute Visual Studio with the installvstemplates swtich to reset the templates available in the new project dialog
            string devEnvFilePath = IDE.FullName + "devenv.exe";
            myProc = Process.Start(devEnvFilePath, "/installvstemplates");

            // wait for the templates to be loaded
            myProc.WaitForExit();

            // close the cmd window
            myProc.Close();

            //throw everything out
            myProc.Dispose();            
        }

		public static string GetVSVersion()
		{
			Console.WriteLine("What version of of Visual Studio are you using?");
			Console.WriteLine("Enter '2005' or '2008'.");

			string vsVersion = Console.ReadLine().Trim();
			if (vsVersion != "2008" && vsVersion != "2005")
			{
				Console.WriteLine("Unknown version. Please try again...");
				Console.WriteLine("");
				vsVersion = GetVSVersion();
			}

			return vsVersion;

		}

		private static string GetVSBitVersion()
		{
			Console.WriteLine("What bit version of of Visual Studio are you using? Enter 'x86' or 'x64'.");

			string vsVersion = Console.ReadLine().Trim().ToUpper();
			if (vsVersion != "X86" && vsVersion != "X64")
			{
				Console.WriteLine("Unknown bit version. Please try again...");
				Console.WriteLine("");
				vsVersion = GetVSBitVersion();
			}

			return vsVersion;
		}

		
    }
}
